#ifndef PACMAN_H
#define PACMAN_H

#include <QMainWindow>
#include<qpainter.h>
#include<QRandomGenerator>
#include<QMessageBox>
#include<QMediaPlayer>
#include<QAudioOutput>

QT_BEGIN_NAMESPACE
namespace Ui {
class pacman;
}
QT_END_NAMESPACE

class pacman : public QMainWindow
{
    Q_OBJECT

//private slots:
  //  void toggleMusic(bool checked);

public:
    pacman(QWidget *parent = nullptr);
    ~pacman();
    void paintEvent(QPaintEvent *event);
    enum GhostMode{NORMAL,SCARED};
    void startGame();
    void pauseGame();



    struct Ghost{
        int x,y;
        Qt::Key direction;
       GhostMode mode=NORMAL;
        QColor color;
    };

private:
   /* QPixmap pacmanRightImg;
    QPixmap pacmanLeftImg;
    QPixmap pacmanUpImg;
    QPixmap pacmanDownImg;

    QPixmap ghostRedImg;
    QPixmap ghostCyanImg;*/

    bool isGameRunning=false;

    QMediaPlayer *musicPlayer;
    QAudioOutput *audioOutput;

    Ui::pacman *ui;
    int score=0;
    const int CELL_SIZE=30;
    static const int ROWS=22;
    static const int COLS=19;
    int gameMap[ROWS][COLS];
    int initMap[ROWS][COLS];
    void  checkCollisions();
    void resetGame();

    void keyPressEvent(QKeyEvent *event);
    void updateAnimation();//更新嘴巴开合动画
    void movePacman();//定时移动
    bool canMoveTo(int x,int y);
    bool canMoveToDirection(int direction);
    bool canChangeDirection();
    void gameUpdate();
    bool isAtGridCenter();

    //吃豆人属性
    int pacmanX=1*CELL_SIZE,pacmanY=1*CELL_SIZE;
    int currentDirection=Qt::Key_Right;//当前移动方向
    int nextDirection=Qt::Key_Right;//下一个输入方向
    int mouthAngle=0;//嘴巴开合角度
    bool isMouthClosing=false;//嘴巴开合状态
    QTimer *gameTimer;//控制游戏循环
    const int PACMAN_SPEED=2;//每帧移动像素数
    bool isMoving=false;//是否正在移动

    //动画计时器
    QTimer *animationTimer;
    QTimer *movementTimer;

    //幽灵
    QVector<Ghost>ghosts;
    QTimer* ghostTimer;
    void moveGhosts();
    bool canGhostMoveTo(const Ghost& ghost,Qt::Key dir);

};
#endif // PACMAN_H
